package vn.edu.saigontech.ncms.web.study;

import vn.edu.saigontech.ncms.model.result.CambridgeTest;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateCambridgeTestAction extends AbstractValidator {
	
	private String[] listenings;
	private String[] speakings;
	private String[] readings;

	public String check() {	
		for(int i = 0; i < this.listenings.length; i++) {
			boolean b1 = false;
			boolean b2 = false;
			boolean b3 = false;
			boolean b4 = false;
			boolean b5 = false;
			if(!Checker.isEmpty(this.listenings[i])) {
				if(!Checker.isDouble(this.listenings[i]) || !Checker.isInRange(Double.valueOf(this.listenings[i]), 0, 100)) {
					this.sendKeyError("validator.invalid", "listening");
				}
				else {
					b1 = true;
				}
			}
			if(!Checker.isEmpty(this.speakings[i])) {
				if(!Checker.isDouble(this.speakings[i]) || !Checker.isInRange(Double.valueOf(this.speakings[i]), 0, 100)) {
					this.sendKeyError("validator.invalid", "speaking");
				}
				else {
					b2 = true;
				}
			}
			if(!Checker.isEmpty(this.readings[i])) {
				if(!Checker.isDouble(this.readings[i]) || !Checker.isInRange(Double.valueOf(this.readings[i]), 0, 100)) {
					this.sendKeyError("validator.invalid", "reading");
				}
				else {
					b4 = true;
				}
			}
			if(b1 && b2 && b3 && b4 && b5) {
				CambridgeTest finalTest = new CambridgeTest();
				finalTest.setListening(Double.parseDouble(this.listenings[i]));
				finalTest.setSpeaking(Double.parseDouble(this.speakings[i]));
				finalTest.setReading(Double.parseDouble(this.readings[i]));
				if(!Checker.isInRange(finalTest.getWrittenScore(), 0, 100)) {
					this.sendKeyError("validator.invalid", "written");
				}
				if(!Checker.isInRange(finalTest.getOralScore(), 0, 100)) {
					this.sendKeyError("validator.invalid", "oral");
				}
			}	
		}
		return this.toSuccess();
	}

	public String[] getListenings() {
		return listenings;
	}

	public void setListenings(String[] listenings) {
		this.listenings = listenings;
	}

	public String[] getSpeakings() {
		return speakings;
	}

	public void setSpeakings(String[] speakings) {
		this.speakings = speakings;
	}

	public String[] getReadings() {
		return readings;
	}

	public void setReadings(String[] readings) {
		this.readings = readings;
	}
}